package com.redkite.app;

import com.redkite.app.common.GeneralActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class SelectImageActivity extends GeneralActivity {
	
	/*Create 2 options to take an image from gallery or camera, assigned with integer values*/
    private static final int SELECT_PICTURE = 1;
    private static final int TAKE_PICTURE = 2;
 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.p_select_image_activity);
    /*button to go to gallery to sellect imgae*/
        ((Button) findViewById(R.id.browse))
                .setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_PICK);
                        startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
                    }
                });
        /*button to take image from device's camera*/
        ((Button) findViewById(R.id.capture))
        .setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, TAKE_PICTURE);
            }
        });
    }
 
    /*
     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
     * 
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
            	/*getting image from gallery and then send it to GetImageActivity.class*/
	                Uri selectedImageUri = data.getData();
	                Intent i = new Intent(this,RedKiteMainScreen.class);
	                i.putExtra("SELECT_PICTURE",selectedImageUri);
	                startActivity(i);
            } else
            if (requestCode == TAKE_PICTURE) {
            	/*getting image from camera and then send it to GetImageActivity.class*/
	            	Bitmap bmp = (Bitmap) data.getExtras().get("data");
	            	Intent i = new Intent(this,UploadToServerActivity.class);
	            	i.putExtra("TAKE_PICTURE",bmp);
	            	startActivity(i);
            } 
    	}
    }
}
